﻿<?
error_reporting(0);
include("../config/header.php");
?>
<script type='text/javascript'>
	document.title = '将代码以JS方式加密、解密 —— 在线工具';
</script>
<div class="container">
	<div class="panel panel-primary">
		<div class="panel-heading"><h3 class="panel-title">将代码以JS方式加密、解密</h3></div>
	</div>
	<div class="form-group">
		<textarea id="content" name="content" style="width:100%;height:300px;border:1px solid #c5e2f2;overflow:visible;"></textarea>
	</div>
	<div class="form-group" align="center">
		<input type="button" class="button button-rounded button-flat-action" onclick="if(this.value=='JS加密'){document.getElementById('content').value='&lt;script&gt;'+jsencode(this)+'&lt;/script&gt;';this.value='JS解密';}else{jsdecode(this);this.value='JS加密';}" value="JS加密">
		<input type="button" onclick="test();" value="测试"  class="button button-rounded button-flat-primary">
	</div>
</div>
<script>
//将代码以JS方式加密、解密
function jsencode(obj) {
	var v = document.getElementById("content").value;
	var es = escape(v);
	return String.format("document.write(unescape('{0}'));", es);
}

function jsdecode(obj) {
	var v = document.getElementById("content").value;
	var regex = /unescape\('([a-z%0-9]*)'\)/i;
	if (v.match(regex)) {
		document.getElementById("content").value = unescape(RegExp.$1);
	}
}

//将代码以JS方式加密、解密
String.format = function () {
    if (arguments.length == 0) return null;
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var regExp = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(regExp, arguments[i])
    }
    return str
};

function test() {
	var win = window.open();
	win.document.open();
	win.document.write(document.getElementById('content').value);
	win.document.close();
}
document.write(tools.replace('undefined',''));
</script>

<?php
include("../config/footer.php");
?>